
package com.shycloud.mido.member.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.api.dto.TeacherApprovalDTO;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.Teacher;
import com.shycloud.mido.member.api.entity.TeacherApproval;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.TeacherApprovalMapper;
import com.shycloud.mido.member.mapper.TeacherMapper;
import com.shycloud.mido.member.service.TeacherApprovalService;

import lombok.AllArgsConstructor;

/**
 * 平台认证教师表
 *
 * @author jtzhang
 * @date 2019-02-27 13:16:46
 */
@Service
@AllArgsConstructor
public class TeacherApprovalServiceImpl extends ServiceImpl<TeacherApprovalMapper, TeacherApproval> implements TeacherApprovalService {
    private TeacherApprovalMapper teacherApprovalMapper;
    private TeacherMapper teacherMapper;
    private MemberMapper memberMapper;

    /**
     * 分页查询教师资质认证
     *
     * @param page    分页对象
     * @param teacherApprovalDTO 参数列表
     * @return
     */
    @Override
    public IPage getTeacherApprovalDTOPage(Page page, TeacherApprovalDTO teacherApprovalDTO) {
        return baseMapper.getTeacherApprovalVosPage(page, teacherApprovalDTO);
    }

    /**
     * 通过审核
     *
     * @param id id
     * @return
     */
    @Override
    public boolean accept(String id){
        boolean a =baseMapper.accept(id);
        TeacherApproval teacherApproval = teacherApprovalMapper.selectById(id);
		teacherApproval.setStatus("1");
		teacherApprovalMapper.updateById(teacherApproval);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id",teacherApproval.getMemberId() );
        teacherMapper.delete(queryWrapper);
        Member member = new Member();
        member.setIsTeacher("1");
        UpdateWrapper<Member> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",teacherApproval.getMemberId());
        memberMapper.update(member,updateWrapper);
        baseMapper.insertTeacherInfo(teacherApproval);
        return a;

    }


	/**
	 * 驳回审核
	 *
	 * @param id
	 * @param remarks
	 * @return
	 */
	@Override
	public boolean reject(String id, String remarks) {

		//主键检索平台认证教师表
		TeacherApproval approval = teacherApprovalMapper.selectById(id);
		// 驳回申请
		boolean reject = baseMapper.reject(id, remarks);
		// 初始化更新【member -> isTeacher 字段】结果
		boolean updIsTeacher = false;
		// 驳回申请成功
		if (reject == true) {
			//更新member 表【是否教师】字段
			updIsTeacher = teacherApprovalMapper.updIsTeacher(approval.getMemberId(), "3");
		}
		//返回执行结果
		return updIsTeacher;
	}

      /**
     * 查询证书照片
     * @param memberId memberId
     * @return
     */
      @Override
      public List<String> getTeacherApprovalProPic (String memberId){
          return teacherApprovalMapper.getTeacherApprovalProPic(memberId);
      }

    /**
     * 查询教师科目Id
     * @param memberId memberId
     * @return
     */
    @Override
   public  List<String> getTeacherApprovalSubjectId(String memberId){
        return teacherApprovalMapper.getTeacherApprovalSubjectId(memberId);
    }
    
}